<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最短无序连续子数组</title>
</head>
<body>
  <script>
    function fn(nums) {
      let ans = nums.slice().sort((a, b) => a - b);
      let res = nums.length, end = 0;
      for (let i = 0; i < nums.length; i++) {
        if (ans[i] !== nums[i]) {
          res = Math.min(res, i);
          end = Math.max(end, i);
        }
      }
      return end - res > 0 ? end - res + 1 : 0;
    }
    console.log(fn([2, 6, 4, 8, 10, 9, 15]))
  </script>
</body>
</html>